//
// GPSMapEdit
// (c) Konstantin Galichsky (kg@geopainting.com), 2002-2005
//
// 'Drag map' tool.
//

# include "StdAfx.h"
# include "Resource.h"
# include "Tool.h"
# include "Map.h"
# include "Settings.h"
# include "Position.h"
# include "MapView.h"
# include "Globals.h"

class CDragTool : public CTool {
  public:
	CDragTool () : CTool (toolDrag) {}

	virtual bool Init ();
	virtual void OnActivate ();

	virtual void OnLDown (int _x, int _y, DWORD _dwKeys);
	virtual void OnLUp   (int _x, int _y, DWORD _dwKeys);
	virtual void OnMove  (int _x, int _y, DWORD _dwKeys);
};

DECLARE_TOOL (CDragTool);

static HCURSOR g_hModeDragCursor;
static HCURSOR g_hDragCursor;
static HCURSOR g_hNoDragCursor;

bool CDragTool::Init () {
	assert (g_hInst);
	g_hModeDragCursor = ::LoadCursor (g_hInst, MAKEINTRESOURCE (IDC_CursorModeDrag));
	g_hDragCursor     = ::LoadCursor (g_hInst, MAKEINTRESOURCE (IDC_CursorDrag));
	g_hNoDragCursor   = ::LoadCursor (g_hInst, MAKEINTRESOURCE (IDC_CursorNoDrag));

	return true;
}

void CDragTool::OnActivate () {
	SetPermanentCursor (g_hModeDragCursor);
}

static long g_lThumbPosX0;
static long g_lThumbPosY0;

void CDragTool::OnLDown (int _x, int _y, DWORD _dwKeys) {
//	if (g_lMapWidth <= g_lWidth && g_lMapHeight <= g_lHeight) {
//		SetPermanentCursor (g_hNoDragCursor);
//	} else {
		SetPermanentCursor (g_hDragCursor);

		StartDND (_x, _y);

		SCROLLINFO si = {sizeof (si), SIF_ALL};
		::GetScrollInfo (g_hMapWnd, SB_HORZ, & si);
		g_lThumbPosX0 = si.nPos;

		::GetScrollInfo (g_hMapWnd, SB_VERT, & si);
		g_lThumbPosY0 = si.nPos;
//	}
}

void CDragTool::OnLUp (int _x, int _y, DWORD _dwKeys) {
	SetPermanentCursor (g_hModeDragCursor);

	if (! CTool::s_bDND)
		return;
	StopDND ();
	ScrollMap (SB_ENDSCROLL, 0, SB_ENDSCROLL, 0);
}

void CDragTool::OnMove (int _x, int _y, DWORD _dwKeys) {
	if (CTool::s_bDND)
		ScrollMap (
			SB_THUMBTRACK, g_lThumbPosX0 + CTool::s_lX_DND_Start - _x,
			SB_THUMBTRACK, g_lThumbPosY0 + CTool::s_lY_DND_Start - _y
		);
}

